package www.ym.com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ym.com.entity.Category;
import www.ym.com.mapper.CategoryMapper;
import www.ym.com.service.CategoryService;
import www.ym.com.service.SetmealService;

@Slf4j
@Transactional
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

//    @Autowired
//    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryMapper categoryMapper;

    public Page<Category> pageCategory(JSONObject jsonObject) {
        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //每页显示数
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));
        //分类名称
        String categoryName = String.valueOf(jsonObject.get("categoryName"));
        //分类类别
        String type = String.valueOf(String.valueOf(jsonObject.get("type")));
        log.info("当前页为" + pageNum);
        log.info("每页显示数" + pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(type!="",Category::getType, type).or().eq(categoryName!="",Category::getCategoryName,categoryName);
        wrapper.orderByDesc(Category::getUpdateTime);
        //进行分页查询-可跟条件
        Page<Category> urf = categoryMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        System.out.println("数据为：" + urf.getRecords());
        System.out.println("总数为：" + urf.getTotal() + ",总页数为：" + urf.getPages());
        System.out.println("当前页为：" + urf.getCurrent() + ",每页限制：" + urf.getSize());
        return urf;
    }

//    /**
//     * 删除分类，判断是否绑定具体菜品和具体套餐
//     * 根据id删除分类，删除之前进行判断
//     * @param id
//     */
//    @Override
//    public void delete(@RequestParam("ids") Long id) {
//
//        LambdaQueryWrapper<Dish> queryWrapper1=new LambdaQueryWrapper<>();
//        //添加查询条件，根据分类id进行查询
//        queryWrapper1.eq(Dish::getCategoryId,id);
//        int count1=dishService.count(queryWrapper1);
//        //查询当前分类是否关联具体菜品，如果关联，抛出业务异常
//        if (count1>0){
//            //已关联菜品，抛出业务异常
//            throw new CustomException("当前菜品分类下已关联具体菜品，无法删除");
//        }
//
//        LambdaQueryWrapper<Setmeal> queryWrapper2=new LambdaQueryWrapper<>();
//        queryWrapper2.eq(Setmeal::getCategoryId,id);
//        int count2=setmealService.count(queryWrapper2);
//        //查询当前分类是否关联具体菜品，如果关联，抛出业务异常
//        if(count2>0){
//            //已关联菜品，抛出业务异常
//            throw new CustomException("当前套餐分类下已关联具体套餐，无法删除");
//        }
//
//        //分类没有进行关联,可以正常删除
//        super.removeById(id);
//
//    }
}

